extends ColorRect
class_name DividerLine

enum Type{HALF, SEVEN}

signal position_change(change_pos:Vector2)
signal hided

const HalfPosition := Vector2(630.0,0.0)
const SevenPosition := Vector2(886.0,0.0)

@onready var direct: TextureRect = $Direct

var fake_pos: Vector2:
	set(v):
		global_position = v
		position_change.emit(v+Vector2(20,0))

var current_npc:Npc:
	set(v):
		current_npc = v
		current_npc.npc_in_screen.connect(on_current_npc_in_screen)

func _ready() -> void:
	GameEvents.stage_entered.connect(on_stage_entered)
	direct.pivot_offset = direct.size/2

func change_position(sec: float = 0.3,type:Type = Type.HALF) -> void:
		var tween = create_tween()
		if type == Type.HALF:
			tween.tween_property(self, "fake_pos", HalfPosition, sec).from(SevenPosition).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
		else:
			tween.tween_property(self, "fake_pos", SevenPosition, sec).from(HalfPosition).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)


func _process(delta: float) -> void:
	if current_npc != null:
		direct.rotation = Global.player.global_position.direction_to(current_npc.global_position).angle()

func on_stage_entered() -> void:
	var tween = create_tween()
	show()
	tween.set_parallel()
	tween.tween_property(self.material,"shader_parameter/progress",-1.1,0.3).from(4).set_ease(Tween.EASE_IN_OUT)\
	.set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(direct,"modulate:a",1,0.3).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.chain()
	
	
	

func on_current_npc_in_screen() -> void:
	var tween = create_tween()
	tween.set_parallel()
	tween.tween_property(self.material,"shader_parameter/progress",4,0.3).from(-1.1).set_ease(Tween.EASE_IN_OUT)\
	.set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(direct,"modulate:a",0,0.3).set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.chain()
	tween.tween_callback(hide)
	


func _on_visibility_changed() -> void:
	if visible == false:
		hided.emit()
